package com.weiwei.diancan.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.weiwei.diancan.Contant.Config;
import com.weiwei.diancan.Contant.ConfigMethod;
import com.weiwei.diancan.bean.LoginBean;
import com.weiwei.diancan.cache.CacheConfig;
import com.weiwei.diancan.http.RetrofitFactory;
import com.weiwei.diancan.view.EmptyLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import weiwei.com.weiweilibrary.views.ToastUtil;

/**
 * Created by BMW on 2017/2/22.
 */
public class GetInfoService extends Service {
    public static boolean index = true;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();

//        new Thread() {
//            @Override
//            public void run() {
//                try {
//                    while (index) {
//                        Thread.sleep(3000);
//                        onRefreshData();
//                    }
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                //onRefreshData();
//            }
//        }.start();
    }

    @Override
    public void onDestroy() {
        index = false;
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }


    private void onRefreshData(){
        Map<String,String> map = new HashMap<>();
        if(TextUtils.isEmpty(CacheConfig.getInst().getToken(this))){
            return;
        }

        if(TextUtils.isEmpty(CacheConfig.getInst().getWaterToken(this))){
            return;
        }
        map.put(ConfigMethod.shop_token,CacheConfig.getInst().getToken(this));
        map.put(ConfigMethod.waiter_token,CacheConfig.getInst().getWaterToken(this));
        RetrofitFactory.getIntance().refreshData(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LoginBean>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(LoginBean loginBean) {
                        if(loginBean.getIs_success().equals(ConfigMethod.Success)) {
                            LoginBean login = CacheConfig.getInst().getLogin();
                            loginBean.setShop_token(CacheConfig.getInst().getToken(GetInfoService.this));
                            loginBean.setName5(login.getName5());
                            CacheConfig.getInst().putData(loginBean);
                            EventBus.getDefault().post(Config.LOGIN_SUCCESS);
                        }else{

                        }
                    }
                });
    }
}
